//
//  LJFHttpManager.m
//  SevenZhou
//
//  Created by qianfeng on 15/9/14.
//  Copyright (c) 2015年 ljf. All rights reserved.
//

#import "LJFHttpManager.h"
#import "AFNetworking.h"


@implementation LJFHttpManager

+ (instancetype)shareInstance
{
    static LJFHttpManager *manager = nil;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
       
        manager = [[LJFHttpManager alloc] init];
        
    });
    
    return manager;
}

- (void)getDataFromNetWithUrl:(NSString *)urlString AndParameterStr:(NSString *)parameter success:(HttpRequestSuccess)success failure:(HttpRequestFialure)failure
{
    [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];
    
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    request.HTTPMethod = @"POST";
    
    request.HTTPBody = [parameter dataUsingEncoding:NSUTF8StringEncoding];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
    {
        if (connectionError)
        {
            failure(connectionError);
            [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
        }
        else
        {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            
            success(dict);
            
            [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
        }
        
    }];
    
}

- (void)getDataFromNetWithUrl:(NSString *)urlString AndParameterStr:(NSString *)parameter andIdString:(NSString *)idStr success:(HttpRequestSuccess)success failure:(HttpRequestFialure)failure
{
    [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];
    
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    request.HTTPMethod = @"POST";
    
    NSString *postStr = [NSString stringWithFormat:parameter,idStr];
    
    request.HTTPBody = [postStr dataUsingEncoding:NSUTF8StringEncoding];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
     {
         if (connectionError)
         {
             failure(connectionError);
             
             [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
         }
         else
         {
             NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
             
             success(dict);
             
             [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
         }
         
     }];

}

- (void)getDataFromNetWithUrl:(NSString *)urlString AndParameterStr:(NSString *)parameter andIdString:(NSString *)idStr andPage:(int)page success:(HttpRequestSuccess)success failure:(HttpRequestFialure)failure
{
    [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];
    
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    request.HTTPMethod = @"POST";
    
    NSString *postStr = nil;
    
    if (page)
    {
        postStr = [NSString stringWithFormat:parameter,page,idStr];
    }
    else
    {
        postStr = [NSString stringWithFormat:parameter,idStr];
    }
    
    request.HTTPBody = [postStr dataUsingEncoding:NSUTF8StringEncoding];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
     {
         if (connectionError)
         {
             failure(connectionError);
             
             [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
         }
         else
         {
             NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
             
             success(dict);
             
             [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
         }
         
     }];
}

- (void)getDataFromNetWithUrl:(NSString *)urlString success:(HttpRequestSuccess)success failure:(HttpRequestFialure)failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    [manager POST:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        success(responseObject);
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {
        failure(error);
        
    }];
}

@end
